package view;

import javax.swing.DefaultListModel;
import javax.swing.JButton;

import javax.swing.JList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import javax.swing.JTextArea;




import net.miginfocom.swing.MigLayout;

/**
 * This class manages the Templates view window
 * 
 * The template view shows all existing templates and allows the user to add, remove or edit templates
 * 
 * @author MUSAB
 * @since May 2013
 * @version $Revision: 1.0 $
 */
public class TemplateView{
	
	/**
	 * Model for the list of templates
	 */
	private DefaultListModel<String> templates;
	
	/**
	 * JList to display the templates
	 */
	private JList templatesList;
	
	/**
	 * Buttons
	 */
	JButton addNewBtn, deleteBtn, editbtn;
	
	/**
	 * Scroll pane for templates list
	 */
	JScrollPane scrollPane;
	
	/**
	 * Body text area
	 */
	JTextArea body;
	
	/**
	 * Constructor, sets up the window
	 */
	public TemplateView(){
		
		initializeComponents();
	}

	/**
	 * Initializes the components in this view
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initializeComponents() {
		
		templates = new DefaultListModel<String>();
		templatesList = new JList();
		templatesList.setModel(templates);
		
		addNewBtn = new JButton("Add new...", IconsManager.getImageIcon("small/plus.png"));		
		editbtn = new JButton("Edit...", IconsManager.getImageIcon("small/document-pencil.png"));
		deleteBtn = new JButton("Delete", IconsManager.getImageIcon("small/cross.png"));
		
		body = new JTextArea();
		body.setEditable(false);
		
		scrollPane = new JScrollPane(templatesList);

	}

	/**
	 * Creates a joption pane with the components and shows it
	 */
	public void showDialog() {
		
		// Create and Layout all the panels
		JPanel container = new JPanel(new MigLayout("fillx"));
		JPanel westPanel = new JPanel(new MigLayout("fillx"));
		container.add(westPanel, "dock west, width 80");
		
		// Adding components to the west panel
		westPanel.add(addNewBtn, "wrap, growx");
		westPanel.add(scrollPane, "growx");
		
		// Adding components to the toolbar panel
		container.add(editbtn, "push, align right");
		container.add(deleteBtn, "wrap,align right");
		
		// Adding components to the main panel
		container.add(body, "growx, span 2, height 200, width 450, height 300");
		
		// Show the popup
		ViewHelpers.showDialog(container, "Manage templates");
	}

	/**
	
	 * @return the templates */
	public DefaultListModel<String> getTemplates() {
		return templates;
	}

	/**
	
	 * @return the templatesList */
	public JList getTemplatesList() {
		return templatesList;
	}

	/**
	
	 * @return the addNewBtn */
	public JButton getAddNewBtn() {
		return addNewBtn;
	}

	/**
	
	 * @return the deleteBtn */
	public JButton getDeleteBtn() {
		return deleteBtn;
	}

	/**
	
	 * @return the editbtn */
	public JButton getEditbtn() {
		return editbtn;
	}

	/**
	
	 * @return the body */
	public JTextArea getBody() {
		return body;
	}

	/**
	 * @param body the body to set
	 */
	public void setBody(JTextArea body) {
		this.body = body;
	}



	
}
